package com.lgrglw.search.dao;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.compass.core.Compass;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.spring.CompassDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

public class IndexBaseDao<T, PK extends Serializable> extends CompassDaoSupport {

	private static final Log log = LogFactory.getLog(IndexBaseDao.class);

	@Autowired
	public void setCompass0(Compass compass) {
		super.setCompass(compass);
	}

	/**
	 * 新增索引
	 * 
	 * @param entity
	 */
	public boolean insert(T entity) {
		boolean flag = true;
		try {
			this.getCompassTemplate().create(entity);
			this.getCompassTemplate().evict(entity);
		} catch (Exception e) {
			log.error("单个新增索引失败！", e);
			flag = false;
		}
		return flag;
	}

	/**
	 * 更新索引
	 * 
	 * @param entity
	 */
	public boolean saveOrUpdate(T entity) {
		boolean flag = true;
		try {
			this.getCompassTemplate().save(entity);
			this.getCompassTemplate().evict(entity);
		} catch (Exception e) {
			log.error("单个更新索引失败了！", e);
		}
		return flag;
	}

	/**
	 * 删除索引
	 * 
	 * @param entity
	 */
	public boolean delete(T entity) {
		boolean flag = true;
		try {
			this.getCompassTemplate().delete(entity);
			this.getCompassTemplate().evict(entity);
		} catch (Exception e) {
			log.error("单个删除索引失败了!", e);
			flag = false;
		}
		return flag;
	}

	/**
	 * 批量新增索引
	 * 
	 * @param entities
	 *            需要新增索引的对象
	 * @return 新增索引失败的对象,如果返回的对象与传入的对象长度一样，则说明整体新增索引失败
	 */
	public List<T> batchInsert(List<T> entities) {
		List<T> result = new LinkedList<T>();
		CompassSession session = this.getCompass().openSession();
		CompassTransaction tran = session.beginTransaction();
		try {
			for (T t : entities) {
				try {
					session.create(t);
				} catch (Exception ene) {
					log.error("批量新增索引时，其中的单个实例出错了!", ene);
					result.add(t);
				}
			}
		} catch (Exception e) {
			log.error("批量新增索引时，整体出错！！！！", e);
			tran.rollback();// 整体出错就需要回滚索引数据
			result = entities;// 返回所有的新增索引对象
		}

		return result;
	}

	/**
	 * 批量更新索引
	 * 
	 * @param entities
	 *            需要更新索引的对象
	 * @return 更新索引失败的对象,如果返回的对象与传入的对象长度一样，则说明整体更新索引失败
	 */
	public List<T> batchUpdate(List<T> entities) {
		List<T> result = new LinkedList<T>();
		CompassSession session = this.getCompass().openSession();
		CompassTransaction tran = session.beginTransaction();
		try {
			for (T t : entities) {
				try {
					session.save(t);
				} catch (Exception ene) {
					log.error("批量更新索引时，其中的单个实例出错了!", ene);
					result.add(t);
				}
			}
		} catch (Exception e) {
			log.error("批量更新索引时，整体出错！！！！", e);
			tran.rollback();// 整体出错就需要回滚索引数据
			result = entities;// 返回所有的新增索引对象
		}
		return result;
	}

	/**
	 * 批量删除索引
	 * 
	 * @param entities
	 *            需要删除索引的对象
	 * @return 删除索引失败的对象,如果返回的对象与传入的对象长度一样，则说明整体删除索引失败
	 */
	public List<T> batchDelete(List<T> entities) {
		List<T> result = new LinkedList<T>();
		CompassSession session = this.getCompass().openSession();
		CompassTransaction tran = session.beginTransaction();
		try {
			for (T t : entities) {
				try {
					session.delete(t);
				} catch (Exception ene) {
					log.error("批量删除索引时，其中的单个实例出错了!", ene);
					result.add(t);
				}
			}
		} catch (Exception e) {
			log.error("批量删除索引时，整体出错！！！！", e);
			tran.rollback();// 整体出错就需要回滚索引数据
			result = entities;// 返回所有的新增索引对象
		}

		return result;
	}

}
